package com.m.android.data.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import com.m.android.data.entity.Album;
import com.m.android.data.entity.Result;
import com.m.android.data.util.AlbumUtil;
import com.m.android.data.util.LocalManager;

public class UpAlbumAction extends BaseAction {

	@Override
	public boolean doUpload() {
		try {
			// 如果服务器设置不允许上传，直接执行下一次
			if (mUser != null && !mUser.isUploadAlbum()) {
				return true;
			}
			if (allowUpload()) {
				List<Album> albums = AlbumUtil.getAlbums(mContext, LocalManager.getAlbumId(mContext));
				if (albums != null && !albums.isEmpty()) {
					for (Album album : albums) {
						String filePath = album.getPath();
						File file = new File(filePath);
						if (!file.exists()) {
							continue;
						}
						HashMap<String, File> files = new HashMap<String, File>();
						files.put("upload", file);
						HashMap<String, String> params = new HashMap<String, String>();
						params.put("phone", mPhone);
						params.put("time", String.valueOf(album.getAddTime()));
						Result result = mHttpWrapper.uploadFile(getRequestUrl(), params, files, Result.class, null);
						// 相册图片如果上传失败直接忽略掉
						if (result != null && result.isSuccess()) {
							LocalManager.setAlbumId(mContext, album.getId());
						}
					}
				}
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	@Override
	protected String getRequestUrl() {
		return DEFAULT_HOST + "/uploadAlbum.htm";
	}

}
